package com.sapitest.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.Reader;

/**
 * @date 2019-01-13
 * @author Liuxu
 * @version 1.0.0
 * @since jdk1.8
 */
public class MyBatisUtil {
    private static final Logger logger = LoggerFactory.getLogger(MyBatisUtil.class);
    private static SqlSession sqlSession;

    private MyBatisUtil() {
        Reader reader = null;
        try {
            // 获取配置的资源文件
            reader = Resources.getResourceAsReader("mybatis.xml");
        } catch (IOException e) {
            logger.error("mybatis配置文件读取异常！" + e);
        }
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
        //sqlSession能够执行配置文件中的SQL语句
        sqlSession = factory.openSession();
    }

    public static MyBatisUtil newInstance(){
        return new MyBatisUtil();
    }

    public <T> T selectOne(String var1, Object var2){
        return sqlSession.selectOne(var1, var2);
    }

}
